package com.app.backend;

import com.app.backend.filter.AccessFilter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import tk.mybatis.spring.annotation.MapperScan;

@Slf4j
@MapperScan(basePackages = "com.app.backend.*.dao")
@EnableTransactionManagement
// 启用缓存
@EnableCaching
// 启用异步
@EnableAsync
// 启用自带定时任务
@EnableScheduling
// 开启配置属性支持
@EnableConfigurationProperties
//启动jpa审计
@EnableJpaAuditing
@EnableWebMvc
@SpringBootApplication(scanBasePackages = "com.app"
        , exclude = {org.activiti.spring.boot.SecurityAutoConfiguration.class,
        SecurityAutoConfiguration.class,
        DataSourceAutoConfiguration.class})
public class BackendMainApplication {

    public static void main(String[] args) {
        SpringApplication.run(BackendMainApplication.class, args);
        log.info("app-backend 服务启动成功...");
    }



    /**
     * 注册允许跨域Filter
     *
     * @return
     */
    @Bean
    protected FilterRegistrationBean registerAccessControlAllowFilter() {
        log.info("注册允许跨域Filter...");
        FilterRegistrationBean registration = new FilterRegistrationBean();
        AccessFilter accessControlFilter = new AccessFilter();
        registration.setAsyncSupported(true);
        registration.setFilter(accessControlFilter);
        return registration;
    }

    @Component
    public class Runner implements CommandLineRunner {
        @Override
        public void run(String... args) throws Exception {
            log.info("app-backend 完成资源初始化");
        }
    }

}
